const app = getApp();
Page({

  data: {
    tabIdx: 0,
    cate: [
      
    ],
    loading: false,
    tip: '上拉加载',
    page: 1,
    cur_list: [],
    is_pull: true,
    id: 0,
    frist: true
  },

  onLoad(e) {
  
    let that = this;

    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          deviceHeight: res.windowHeight
        });
      }
    });
    wx.setNavigationBarTitle({
      title: e.title ? e.title : '任务列表'
    });
    this.getCate((res) => {
      if (app.util.isArray(res)) {
        setTimeout(function() {
          let cate_id = that.get_cate_id();
          if (cate_id != null) {
            res.length > 0 && that.getTask(cate_id);
          } else {
            that.data.id = res[0].id;
         
            res.length > 0 && that.getTask(res[0].id);
          }
        })
      }
    });
  },
  get_cate_id() {
    if (!app.util.isUndefined(app.globalData.cate_id)) {
      let cate_id = app.globalData.cate_id;
      app.globalData.cate_id = null;
      
      this.data.cate.forEach((v, i) => {
        
        if (v.id == cate_id) {
          this.setData({
            tabIdx:i
          })
        }
      });
      return cate_id;
    }
    return null;
  },
  //获取专题分类
  getCate(fn = null) {
    let that = this;
    app.http.$ajax({
      url: 'v2.task/getCate',
      loading: false
    }, {
        success(res) {
          that.setData({
            cate: res.data
          });
          if (fn !== null && app.util.isFunction(fn)) {
            fn(res.data); 
          }
        }
      });
  },
  //获取专题列表
  getTask(zbcategory_id) {
    //如果 is_pull = false代表不能继续上拉加载
    if (!this.data.is_pull) {
      return;
    }
    let that = this;
    this.setData({
      loading: true,
      tip: '正在加载',
    });
    app.http.$ajax({
      url: 'v2.task/getTask',
      data: {
        zbcategory_id: zbcategory_id,
        page: that.data.page
      },
      loading: false
    }, {
        success(res) {
          let newData = res.data.data;
          that.data.cur_list.push(...app.processImg(newData))
          that.setData({
            cur_list: that.data.cur_list
          })
          that.data.page++;
        },
        complete(res) {
          let tip = '';
          if (app.util.isString(res)) {
            app.info(res);
            tip = '上拉加载';
          } else if (!app.util.isUndefined(res.data) && app.util.isArray(res.data.data)) {
            if (res.data.data.length < 15) {
              that.data.is_pull = false;
              tip = '没有更多课程'
            } else {
              tip = '上拉加载';
            }
          }
          that.setData({
            loading: false,
            tip: tip,
          })
        }
      });
  },
  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    if (this.data.frist == false) {
      let cate_id = this.get_cate_id();
      if (cate_id != null) {
        this.data.id = cate_id;
        this.getTask(cate_id);
      }
    } 
    this.data.frist = false;
  },
  //变化tab 请求不同的方法
  handleTab(e) {
    let idx = e.currentTarget.dataset.idx;
    if (this.data.tabIdx == idx) {
      return;
    }
    //切换tab 清空当前tab
    this.setData({
      tabIdx: idx,
      cur_list: []
    })

    //初始化
    this.data.page = 1;
    this.data.is_pull = true;
    this.data.id = e.currentTarget.dataset.id;
    //专题分发 获取当前专题special_id
    this.getTask(this.data.id);
  },
  getMore: function () {

    if (this.data.is_pull === true) {
      this.getTask(this.data.id);
    }
  },
  open(e) {
    
  },

  handleTeacher(e) {
    
  }
})